package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/searchs")
public class SearchController {

    @Autowired
    private IStrategyDetailService strategyDetailService;//1

    @Autowired
    private ITravelService travelService;//2

    @Autowired
    private INewsService newsService;//3

    /**
     * 获取搜索类型的接口(攻略/游记/日报)
     * 1.资源 /searchs/count
     * 2.请求方式 get
     * 3.请求参数 keyword
     * 4.返回结果 json
     */
    @GetMapping("/count")
    public Map<String, Object> count(SearchQueryObject qo) {
        HashMap map = new HashMap();
        //查询出strategyDetail中符合搜索条数
        int i1 = strategyDetailService.globalNumberSearch(qo);
        map.put("strategyCount", i1);
        //查询出travel中符合搜索条数
        int i2 = travelService.globalNumberSearch(qo);
        map.put("travelCount", i2);
        //查询出news中符合搜索条数
        int i3 = newsService.globalNumberSearch(qo);
        map.put("newsPageCount", i3);
        return map;
    }

    /**
     * 返回搜素封面和内容资源的接口
     * 1.资源 /searchs
     * 2.请求方式 get
     * 3.请求参数 type keyword
     * 4.返回结果 json
     */
    @GetMapping
    public PageInfo search(SearchQueryObject qo) {
        //创建map
        //Map map = new HashMap();
        //获取type类型
        Integer type = qo.getType();
        if (type != null) {
            //判断type来进行分类搜索
            if (type == 1) {//攻略
                return strategyDetailService.globalSearch(qo);
            } else if (type == 2) {//游记
                return travelService.globalSearch(qo);
            } else if (type == 3) {//日报
                return newsService.globalSearch(qo);
            }
        } else {//没有类型传入判断
            if (strategyDetailService.globalNumberSearch(qo) > 0) {//大于零进行攻略查询
                return strategyDetailService.globalSearch(qo);

            } else if (travelService.globalNumberSearch(qo) > 0) {//大于零进行游记查询
                return travelService.globalSearch(qo);

            } else if (newsService.globalNumberSearch(qo) > 0) {//大于零进行日报查询
                return newsService.globalSearch(qo);
            }
        }
        return null;
    }

}
